<?php

require_once(WSB_XTENSION_PATH .'login/forms/ForgotPassword.php');
require_once(WSB_XTENSION_PATH .'login/forms/ResetPassword.php');

class Login_ForgotPasswordController extends WSB_Xtension_Static
{

	/**
	 * Initialisierung
	 */
	public function init()
	{
		$senderEmail = $this->_getSenderEmail();
		if(empty($senderEmail)) {
			$this->addContent('<div style="background:red;color:white;">Configuration error: No or invalid sender (senderEmail).</div>');
		}
		$this->initTranslate();
	}

	/**
	 * "Passwort vergessen" Formular
	 *
	 * @param Login_Form_ForgotPassword $form
	 */
	public function indexAction(Login_Form_ForgotPassword $form = null)
	{
		if(is_null($form)) {
			$form = new Login_Form_ForgotPassword();
		}
		$this->assignToView('form', $form);
		$this->renderView('forgot_password/index.phtml');
	}

	/**
	 * E-Mail mit Instruktionen
	 */
	public function sendAction()
	{
		$request = $this->getRequest();
		$form = new Login_Form_ForgotPassword();
		if($request->isPost() && $form->isValid($request->getPost())) {
			$this->_sendEmail($form->getValue('email'));
			$this->renderView('forgot_password/send_success.phtml');
		} else {
			$this->indexAction($form);
		}
	}

	/**
	 * Passwort zuruecksetzen Formular
	 */
	public function resetAction()
	{
		$feuserId = intval($this->getParam('id', 0));
		$authCode = trim($this->getParam('authCode', ''));

		// Parameter falsch
		if(($feuserId < 1) || ($authCode == '')) {
			$this->assignToView('error', 'Die notwendigen Parameter fehlen oder sind ung&uuml;ltig.');
			$this->renderView('forgot_password/error.phtml');
			return;
		}

		$feuser = $this->_fetchFeuser($feuserId);
		// Kein Benutzer mit dieser ID vorhanden
		if(!$feuser instanceof Member_Model_DbTable_Feuser_Row) {
			$this->assignToView('error', 'Es ist kein Benutzer mit dieser ID vorhanden.');
			$this->renderView('forgot_password/error.phtml');
			return;
		}
		$this->assignToView('username', $feuser->username);
		$this->assignToView('firstname', $feuser->firstname);
		$this->assignToView('lastname', $feuser->lastname);

		// Authcode falsch
		if($authCode != $feuser->getAuthCode()) {
			$this->assignToView('error', 'Die Authentifizierung ist fehlgeschlagen.');
			$this->renderView('forgot_password/error.phtml');
			return;
		}

		// Formular anzeigen
		$request = $this->getRequest();
		$form = new Login_Form_ResetPassword();
		if($request->isPost() && $form->isValid($request->getPost())) {
			$feuser->password = $form->getValue('password');
			$feuser->save();
			$this->renderView('forgot_password/reset_success.phtml');
		} else {
			$this->assignToView('id', $feuserId);
			$this->assignToView('authCode', $authCode);
			$this->assignToView('form', $form);
			$this->renderView('forgot_password/reset.phtml');
		}

	}

	/**
	 * @param string $email
	 */
	private function _sendEmail($email)
	{
		$feusers = $this->_fetchFeusers($email);
		$this->assignToView('feusers', $feusers);
		$this->_view->email = $email;
		$bodyHtml = $this->_view->render('forgot_password/send_htmlmail.phtml');
		$mail = new Zend_Mail();
		$mail->setBodyHtml($bodyHtml);
		$mail->setBodyText(strip_tags($bodyHtml));
		$mail->setFrom($this->_getSenderEmail(), $this->_getSenderName());
		$mail->addTo($email);
		$mail->setSubject(sprintf($this->translate('loginForgotPasswordEmailSubject'), $_SERVER['HTTP_HOST']));
		$mail->send();
	}

	/**
	 * @param string $email
	 * @return Member_Model_DbTable_Feuser_Rowset
	 */
	private function _fetchFeusers($email)
	{
		$table = new Member_Model_DbTable_Feuser();
		$select = $table->select();
		$select->where('deleted=?', 0);
		$select->where('email=?', $email);
		return $table->fetchAll($select);
	}

	/**
	 * @param integer $id
	 * @return Member_Model_DbTable_Feuser_Row
	 */
	private function _fetchFeuser($id)
	{
		$table = new Member_Model_DbTable_Feuser();
		$select = $table->select();
		$select->where('deleted=?', 0);
		$select->where('id=?', $id);
		return $table->fetchRow($select);
	}

	/**
	 * @return string
	 */
	private function _getSenderEmail()
	{
		$config = $this->getConfiguration();
		$senderEmail = isset($config->senderEmail) ? $config->senderEmail : '';
		return WSB_Div::validEmail($senderEmail) ? $senderEmail : '';
	}

	/**
	 * @return string
	 */
	private function _getSenderName()
	{
		$config = $this->getConfiguration();
		return isset($config->senderName) ? $config->senderName : '';
	}

}
